package casa.depesos.app.com.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;

import casa.depesos.app.com.App;
import casa.depesos.app.com.databinding.ActivityAccountBinding;
import casa.depesos.app.com.dlg.BaseDialogFragment;
import casa.depesos.app.com.dlg.ReadDlg;
import casa.depesos.app.com.http.ReqCallback;
import casa.depesos.app.com.http.bean.GetCodeBean;
import casa.depesos.app.com.http.req.LogoutReq;
import casa.depesos.app.com.utils.Constant;
import casa.depesos.app.com.utils.Utils;

public class AccountActivity extends BaseActivity {

    ActivityAccountBinding binding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityAccountBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        binding.top.back.setOnClickListener(view -> finish());
        binding.top.top.setPadding(0, Utils.getStatusBarHeight(this), 0, 0);
        binding.top.title.setText("Gestión de cuenta");

        if (Constant.white) {
            binding.changePwd.setVisibility(View.INVISIBLE);
        }
        binding.changePwd.setOnClickListener(v -> {
            Intent i = new Intent(AccountActivity.this, ChangePwdActivity.class);
            startActivity(i);
        });

        binding.logout.setOnClickListener(v -> {
            new ReadDlg().setMsg("¿Está seguro de que desea cerrar sesión?", "Cancelar", "Cerrar sesión")
                    .setOnDismissListener(new BaseDialogFragment.OnDismissListener() {
                        @Override
                        public void onDismiss(boolean isOk, Object value) {
                            if (isOk) {
                                logout();
                            }
                        }
                    }).show(getSupportFragmentManager(), "");
        });
    }

    private void logout() {
        showLoading();
        new LogoutReq().req(new ReqCallback<GetCodeBean>() {
            @Override
            public void callback(boolean success, GetCodeBean bean) {
                hideLoading();
                if (Constant.white || success && bean.isValid()) {
                    App.getApp().reLog();
                }
            }
        });
    }

    @Override
    protected boolean needPadding() {
        return false;
    }
}
